package controller;

import java.io.IOException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import model.ReservemsgModel;
import service.ReserveService;
import service.RoomService;
import service.impl.ReserveServiceImpl;
import service.impl.RoomServiceImpl;

/**
 * Servlet implementation class ReserveServlet
 */
@WebServlet("/servlet/ReserveServlet")
public class ReserveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReserveServlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		RoomService roomService = new RoomServiceImpl();
		ReserveService reserveService = new ReserveServiceImpl();

		ReservemsgModel reservemsg = new ReservemsgModel();
		reservemsg.setHotelid(1);
		reservemsg.setRoomtype(request.getParameter("roomType"));
		reservemsg.setReserveid(1);
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		Date sT = new Date();
		Date eT = new Date();
		int staytime=0;
		try {
			sT = sdf.parse(startTime);
			eT = sdf.parse(endTime);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		staytime=(int)Math.abs((eT.getTime() - sT.getTime())/(24*3600*1000));
		if(staytime<=0){
			staytime=1;
		}
		reservemsg.setStaytime(staytime);
		int roomQty=Integer.parseInt(request.getParameter("roomtypeqty"));
		reservemsg.setRoomqty(roomQty);
		int roomIndex=Integer.parseInt(request.getParameter("roomIndex"));
		if(roomIndex>roomQty){
			JOptionPane.showMessageDialog(null, "请输入正确的房间数");
			response.sendRedirect("/HotelSystem/Reserve.jsp");
//			request.getRequestDispatcher("/HotelSystem/Reserve.jsp").forward(request, response);
			return;
		}
		reservemsg.setRoomindex(roomIndex);
		reservemsg.setAllprice(roomIndex*staytime*(Double.parseDouble(request.getParameter("roomPrice"))));
		reservemsg.setR_people(request.getParameter("bookPerson"));
		reservemsg.setPeoplephone(request.getParameter("peoplephone"));
		reservemsg.setPeopleemail(request.getParameter("peopleemail"));
		reservemsg.setUsername((String)request.getSession().getAttribute("username"));
		try {
			reserveService.addReserveMsg(reservemsg);
			roomService.reduceEmptyRoom(request.getParameter("roomType"),roomIndex);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		response.sendRedirect("../userlist.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
